package base

import (
	"MoSkeleton/framework"
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/utils"
	"MoSkeleton/web/pogos/request/monitor"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type CtrlTitle interface {
	GetCtrlTitle() string
}
type BaseCtrl struct {
}

func (b *BaseCtrl) IgnoreLogResponseData(c *gin.Context) {
	var operLog4GinReq *monitor.OperLog4GinReq
	_ = utils.GinGet(c, consts.GinContext_OperLog_key, &operLog4GinReq)
	if operLog4GinReq == nil {
		return
	}
	operLog4GinReq.SaveResponseData = false
}

func (b *BaseCtrl) IgnoreLogRequestData(c *gin.Context) {
	var operLog4GinReq *monitor.OperLog4GinReq
	_ = utils.GinGet(c, consts.GinContext_OperLog_key, &operLog4GinReq)
	if operLog4GinReq == nil {
		return
	}
	operLog4GinReq.SaveRequestData = false
}

func (b *BaseCtrl) InitCtrl(ctrlTitleInst CtrlTitle) gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			c.Next()
		}()
		var operLog4GinReq *monitor.OperLog4GinReq
		err := utils.GinGet(c, consts.GinContext_OperLog_key, &operLog4GinReq)
		if err != nil {
			framework.MoLogger.Error("未找到操作日志信息", zap.Error(err))
			return
		}
		if operLog4GinReq == nil {
			framework.MoLogger.Error("未找到操作日志信息")
			return
		}
		operLog4GinReq.Title = ctrlTitleInst.GetCtrlTitle()
	}
}
